<?php

/**
 * Razred form.
 *
 * @package    dnevnik
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormTemplate.php 10377 2008-07-21 07:10:32Z dwhittle $
 */
class RazredForm extends BaseRazredForm
{
  public function configure()
  {
      $this->setWidget('tip', new sfWidgetFormInputHidden());
      $this->setWidget('profesor_id', new sfWidgetFormInputHidden());
      $this->setWidget('ucenik_razred_list', new sfWidgetFormInputHidden());
      $this->setWidget('jezik', new sfWidgetFormInputHidden());
      $this->setWidget('jezik_profesor', new sfWidgetFormInputHidden());

      $this->setValidator('ucenik_razred_list', new sfValidatorPass());

      if($this->getObject()->getId())
      {
        if($this->getObject()->getSmer()->getTip() != SmerPeer::$tip['grupni'])
        {
          // STRANI JEZIK
          $stranis = array();
          $stranis[''] = sfContext::getInstance()->getI18N()->__("Izaberi");
          $strani_arrs = DbFinder::from('Predmet')->where('Tip', PredmetPeer::$tip['jezik'])->find();
          foreach($strani_arrs as $strani_arr)
            $stranis[$strani_arr->getId()] = (string) $strani_arr;
          $this->setWidget('jezik', new sfWidgetFormChoice(array('choices' => $stranis)));
          $this->setWidget('jezik_profesor', new sfWidgetFormPropelChoice(array('model' => 'Profesor', 'add_empty' => true)));
          $this->widgetSchema['jezik_profesor']->setOption('renderer_class', 'sfWidgetFormPropelJQueryAutocompleter');
          $this->widgetSchema['jezik_profesor']->setOption('renderer_options', array(
            'model' => 'Profil',
            'url'   => '/backend.php/profesor/ajaxList?skola_id='.$this->getObject()->getSkolaId(),
            ));
        }

        // SPISAK UCENIKA ZA GRUPE
        if($this->getObject()->getSmer()->getTip() == SmerPeer::$tip['grupni'])
        {
          $this->setWidget('ucenik_razred_list', new sfWidgetFormChoiceMany(array('choices' => UcenikPeer::getUcenikGrupaList($this->getObject()->getSkolaId()))));
          $this->setValidator('ucenik_razred_list', new sfValidatorPropelChoiceMany(array('model' => 'Ucenik', 'required' => false)));
        }
        // RAZREDNI
        $this->setWidget('profesor_id', new sfWidgetFormPropelChoice(array('model' => 'Profesor', 'add_empty' => true)));
        $this->widgetSchema['profesor_id']->setOption('renderer_class', 'sfWidgetFormPropelJQueryAutocompleter');
        $this->widgetSchema['profesor_id']->setOption('renderer_options', array(
          'model' => 'Profil',
          'url'   => '/backend.php/profesor/ajaxList?skola_id='.$this->getObject()->getSkolaId(),
          ));

        // PREDMETI
        $predmet_razreds = $this->getObject()->getPredmetRazreds();
        foreach($predmet_razreds as $predmet_razred)
        {
          if($predmet_razred->getPredmet()->getTip() == PredmetPeer::$tip['placeholder'])
            continue;
          $pr_form = new PredmetRazredForm($predmet_razred);
          $pr_form->setWidget('razred_id', new sfWidgetFormInputHidden());
          $pr_form->setWidget('predmet_id', new sfWidgetFormInputHidden());

          $pr_form->widgetSchema['profesor_id']->setOption('renderer_class', 'sfWidgetFormPropelJQueryAutocompleter');
          $pr_form->widgetSchema['profesor_id']->setOption('renderer_options', array(
            'model' => 'Profil',
            'url'   => '/backend.php/profesor/ajaxList?skola_id='.$this->getObject()->getSkolaId(),
          ));

          $this->embedForm(str_replace(' ', '_', $predmet_razred->getPredmet()), $pr_form);
        }

        // RASPOREDI
        /*$raspored_tips = DbFinder::from('Raspored')->select('Tip')->where('RazredId', $this->getObject()->getId())->find();

        foreach($raspored_tips as $raspored_tip)
        {
            $raspored_form = new sfForm();
            $raspored_form->widgetSchema->setFormFormatterName('list');

            $rasporeds = DbFinder::from('Raspored')->where('RazredId', $this->getObject()->getId())->where('Tip', $raspored_tip)->orderBy('Dan')->find();
            $i = 1;
            foreach($rasporeds as $raspored)
            {
                $r_f = new RasporedForm($raspored);
                $r_f->setWidget('razred_id', new sfWidgetFormInputHidden());
                $r_f->setWidget('dan', new sfWidgetFormInputHidden());
                $r_f->setWidget('tip', new sfWidgetFormInputHidden());
                $j = -1;
                while($j < 9)
                {
                    if($j < 0)
                    {
                        $r_f->widgetSchema->setLabel('cas_p', ' ');
                        $j++;
                    }
                    else
                      $r_f->widgetSchema->setLabel('cas_'.$j++, ' ');
                }

                $raspored_form->embedForm(Functions::$dani[$raspored->getDan()], $r_f);
            }
            $this->embedForm('Raspored '.$raspored_tip, $raspored_form);
        }*/
      }

      // LABELE
      $this->widgetSchema->setLabel('profesor_id', 'Odeljenjenski starešina');
      $this->widgetSchema->setLabel('smer_id', 'Smer');
      $this->getWidgetSchema()->setLabel('slovo', 'Odeljenje');
      $this->getWidgetSchema()->setLabel('jezik', 'Strani jezik');
      $this->getWidgetSchema()->setLabel('jezik_profesor', 'Profesor stranog jezika');
  }
}